package de.tabuma.ssoclient.logik;

import java.util.*;
import de.tabuma.ssoclient.entity.Customer;

/**
 *
 * @author alefen
 */
public class CustomerHashFacade {
    private static Hashtable<String,List<Customer>> hash = new Hashtable<String, List<Customer>>();

    public synchronized static void put(Customer customer,String ssoId) {
        List<Customer> cList = hash.get(ssoId);
        if(cList==null) {
            cList = new ArrayList<Customer>();
        }
        for(int i=0;i<cList.size();i++) {
            Customer customerListElement = cList.get(i);
            if(customerListElement.getId().intValue()==customer.getId().intValue()) {
                cList.set(i, customer);
                hash.put(ssoId, cList);
                return;
            }
        }
        cList.add(customer);
        hash.put(ssoId, cList);
    }

    public synchronized static Customer get(int id, String ssoId) {
        List<Customer> cList = hash.get(ssoId);
        if(cList==null) {
            return null;
        }
        for(int i=0;i<cList.size();i++) {
            Customer customerListElement = cList.get(i);
            if(customerListElement.getId().intValue()==id) {
                return customerListElement;
            }
        }
        return null;
    }

    public synchronized static List<Customer> getList(String ssoId) {
        return hash.get(ssoId);
    }

    public synchronized static void addList(List<Customer> cList, String ssoId) {
        hash.put(ssoId, cList);
    }

    public synchronized static void remove(int id, String ssoId) {
        List<Customer> cList = hash.get(ssoId);
        if(cList==null) {
            return;
        }
        for(int i=0;i<cList.size();i++) {
            Customer customerListElement = cList.get(i);
            if(customerListElement.getId().intValue()==id) {
                cList.remove(id);
                hash.put(ssoId, cList);
                return;
            }
        }
    }
}
